package com.situ.blog.interceptor;

import com.situ.blog.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {

    //在这个拦截所有浏览器过来的请求
    // return true：代表已经登录，可以访问后台资源
    // return false：代表没有登录，跳转到登录界面
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        if (user != null) {
            return true;
        } else {
            //没有登录，跳转到登录界面
            response.sendRedirect(request.getContextPath() + "/login/getLoginPage");
            return false;
        }
    }
}
